package com.tjmc.company.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.company.api.entity.ResumeInvitationRecordsEntity;
import com.tjmc.company.api.enums.ResumeRecordStatusEnum;
import com.tjmc.company.mapper.ResumeInvitationRecordsMapper;
import com.tjmc.company.service.ResumeInvitationRecordsService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 面试邀请记录表
 *
 * @author recruit
 * @date 2024-11-14 17:07:57
 */
@Service
public class ResumeInvitationRecordsServiceImpl extends ServiceImpl<ResumeInvitationRecordsMapper, ResumeInvitationRecordsEntity> implements ResumeInvitationRecordsService {
    @Async
    @Scheduled(cron = "0 0 1 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void changeInterviewStatus() {
        // 1. 查询过期数据
        List<ResumeInvitationRecordsEntity> expiredList = baseMapper.selectList(new LambdaQueryWrapper<ResumeInvitationRecordsEntity>()
                .eq(ResumeInvitationRecordsEntity::getResumeStatus, ResumeRecordStatusEnum.WAIT_CONFIRM)
                .lt(ResumeInvitationRecordsEntity::getExpiredAt, DateUtil.now()));
        if (expiredList.isEmpty()) {
            return;
        }
        // 2. 修改数据状态为过期
        expiredList.forEach(resumeInvitationRecordsEntity -> {
            resumeInvitationRecordsEntity.setResumeStatus(ResumeRecordStatusEnum.EXPIRED);
            resumeInvitationRecordsEntity.setResumeStatusDesc(ResumeRecordStatusEnum.EXPIRED.getDesc());
        });
        // 3. 更新数据
        updateBatchById(expiredList);
    }

}
